package com.object.module.cbb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;
import com.object.dao.cbb.CddRechargeDetailsDao;
import com.object.module.cbb.entity.CddRechargeDetailsEntity;
import com.object.module.cbb.service.CbbUserService;
import com.object.module.cbb.service.CddRechargeDetailsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("cddRechargeDetailsService")
public class CddRechargeDetailsServiceImpl extends ServiceImpl<CddRechargeDetailsDao, CddRechargeDetailsEntity> implements CddRechargeDetailsService {

    @Autowired
    private CbbUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CddRechargeDetailsEntity> wrapper = new QueryWrapper<>();
        String urId = params.get("urId").toString();
        String rdStatus = params.get("rdStatus").toString();
        if (!StringUtils.isEmpty(urId)) {
            wrapper.eq("rd_ur_id", urId);
        }
        if (!StringUtils.isEmpty(rdStatus)) {
            wrapper.eq("rd_status", rdStatus);
        }
        wrapper.orderByDesc("rd_create_time");
        IPage<CddRechargeDetailsEntity> page = this.page(
                new Query<CddRechargeDetailsEntity>().getPage(params),
                wrapper
        );

        for (CddRechargeDetailsEntity record : page.getRecords()) {
            Long rdUrId = record.getRdUrId();
            record.setUser(userService.getById(rdUrId));
        }

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageht(Map<String, Object> params) {
        QueryWrapper<CddRechargeDetailsEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            wrapper.eq("rd_uuid", key);
        }

        IPage<CddRechargeDetailsEntity> page = this.page(
                new Query<CddRechargeDetailsEntity>().getPage(params),
                wrapper
        );

        for (CddRechargeDetailsEntity detailsEntity : page.getRecords()) {
            detailsEntity.setUser(userService.getById(detailsEntity.getRdUrId()));
        }

        return new PageUtils(page);
    }

}